package itstudio.instructor.fragment;

import itstudio.app.wq.R;
import itstudio.instructor.adapter.CollectListAdapter;
import itstudio.instructor.config.Config;
import itstudio.instructor.config.ImageOptionsUtil;
import itstudio.instructor.entity.News;
import itstudio.instructor.http.TwitterRestClient;
import itstudio.instructor.util.LoginUtil;
import itstudio.instructor.xlistview.XListView;
import itstudio.instructor.xlistview.XListView.IXListViewListener;
import java.util.List;
import org.apache.http.Header;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * @Description 收藏 Fragment
 * 
 * @author MR.Wang
 * 
 * @date 2014-7-5 上午1:13:26
 * 
 * @version V1.0
 */

@SuppressLint("ValidFragment")
public class FragmentCollect extends Fragment implements IXListViewListener {

	private View rootView;
    private LinearLayout loadLayout;
    private LinearLayout loadNoneLayout;
	private CollectListAdapter adapter;
	View parentView;
	private XListView xListview;
	DisplayImageOptions options;
	private List<News> newsList;
	public static FragmentCollect mactivity;
	//
	private int page=0;
	// 获取到下载url后，直接复制给MapApp,里面的全局变量


	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		options = ImageOptionsUtil.headImageOptions();
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mactivity = this;

		rootView = inflater.inflate(R.layout.fragment_collect, container,false);
		Handler handler = new Handler();
		handler.postDelayed(run,400);
		return rootView;

	}
	
	Runnable run = new  Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			initView(rootView);
		}
	};
	private void initView(View parentView) {
		// TODO Auto-generated method stub

		xListview = (XListView) parentView.findViewById(R.id.listview_collect);

		// 获取屏幕分辨率
		DisplayMetrics dm = new DisplayMetrics();
		// getWindowManager().getDefaultDisplay().getMetrics(dm); 只能在activity中用
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		// 加载延迟
		adapter = new CollectListAdapter(getActivity(), newsList, options,
				dm.widthPixels);
		AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(
				adapter);
		xListview.setPullLoadEnable(true);
		xListview.setPullRefreshEnable(false);// 取消下拉刷新
		
		animAdapter.setAbsListView(xListview);
		animAdapter.setInitialDelayMillis(100);
		xListview.setXListViewListener(this);
		xListview.setAdapter(animAdapter);
		
		loadLayout = (LinearLayout) parentView.findViewById(R.id.view_loading);
		loadNoneLayout = (LinearLayout) parentView.findViewById(R.id.view_load_nodata);
	    loadCollect();

	}

	//加载news
	private void loadCollect(){
		
		RequestParams params = new RequestParams();
		params.put("page", page+1);
		TwitterRestClient.post(Config.AC_COLLECT_LIST, params, new AsyncHttpResponseHandler(){
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				onLoad();
				loadLayout.setVisibility(View.GONE);
				loadNoneLayout.setVisibility(View.GONE);
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] data) {
				//加载成功
				loadLayout.setVisibility(View.GONE);
				onLoad();
				Gson gson = new GsonBuilder()  
				  .setDateFormat("yyyy-MM-dd HH:mm:ss")  
				  .create();
				String response = new String(data);
				
				//没登陆
				if(response.indexOf(Config.LOGIN)!=-1 && response.length()<12){
					LoginUtil.Login(getActivity());
				}
				// 有数据
				else {
					newsList  = gson.fromJson( new String(data), new TypeToken<List<News>>(){}.getType());
					adapter.appendToList(newsList);
					
					if(adapter.getCount()==0){
						loadNoneLayout.setVisibility(View.VISIBLE);
					}
					if(newsList!=null  && newsList.size()==10){
						xListview.setPullLoadEnable(true);
						page =page+1;
					}
					else{
						xListview.setPullLoadEnable(false);
						
					}
				}

			}});
	}
	
	// 设置下拉刷新时间
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		 //loadNews();
	}

	// 设置loadmore刷新时间
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		 loadCollect();

	}

	private void onLoad() {
		xListview.stopRefresh();
		xListview.stopLoadMore();
	}
}
